package com.intpro.demo.Controller;

import com.intpro.demo.DAO.CommonRep;
import com.intpro.demo.DAO.Mysql.Model.Paper;
import com.intpro.demo.DAO.User;
import com.intpro.demo.Service.PaperService;
import com.intpro.demo.Service.UserService;
import com.intpro.demo.Utils.CodeException;
import com.intpro.demo.Utils.MyAnnotations.Authorization;
import com.intpro.demo.Utils.MyAnnotations.CurrentUser;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("api/paper")
public class PaperController
{

	@Autowired
	private PaperService paperService;
	@Autowired
	private UserService userService;

	@RequestMapping(value = "getPaper", method = RequestMethod.POST)
	public CommonRep getPaper(@RequestBody Map<String, String> req) throws Exception
	{
		CommonRep rep = new CommonRep();
		int pid = Integer.parseInt(req.get("id"));
		rep.data = paperService.getPaper(pid);
		return rep;
	}

	@RequestMapping(value = "getRandPaper", method = RequestMethod.POST)
	public CommonRep getRandPaper() throws Exception
	{
		CommonRep rep = new CommonRep();
		List<Paper> l = new LinkedList<>();
		Random r = new Random();
		for (int i = 0; i < 4; i++)
		{
			l.add(paperService.getPaper(r.nextInt(675)+1));
		}
		rep.data = l;
		return rep;
	}

	@RequestMapping(value = "getExpert", method = RequestMethod.POST)
	public CommonRep getExpert(@RequestBody Map<String, String> req) throws Exception
	{
		CommonRep rep = new CommonRep();
		int pid = Integer.parseInt(req.get("id"));
		rep.data = paperService.getExpert(pid);
		return rep;
	}

	@Authorization
	@RequestMapping(value = "subscribe", method = RequestMethod.POST)
	public CommonRep subscribe(@CurrentUser User u, @RequestBody Map<String, String> req)
			throws Exception
	{
		CommonRep rep = new CommonRep();
		long pid = Long.parseLong(req.get("pid"));
		if (u.subscribe == null)
		{
			u.subscribe = new ArrayList<>();
		}
		u.subscribe.add(pid);
		userService.updateUser(u);
		return rep;
	}

	@RequestMapping(value = "getPaperByExpert",method = RequestMethod.POST)
	public CommonRep getPaperByExpert(@RequestBody Map<String, String> req)
			throws Exception
	{
		CommonRep rep = new CommonRep();

		var eid = req.get("id");
		if(eid == null)
		{
			throw CodeException.NotNullErr();
		}
		rep.data = paperService.getPaperByExpert(Integer.parseInt(eid));
		return rep;
	}

	@RequestMapping(value = "getRelatedExpert",method = RequestMethod.POST)
	public CommonRep getRelatedExpert(@RequestBody Map<String, String> req)
			throws Exception
	{
		CommonRep rep = new CommonRep();

		var eid = req.get("id");
		if(eid == null)
		{
			throw CodeException.NotNullErr();
		}
		rep.data = paperService.getRelatedExpert(Integer.parseInt(eid));
		return rep;
	}
}
